package com.googlecode.lightest.core

class SimpleDispatcherAssignmentStrategy
    implements IDispatcherAssignmentStrategy
{
    List<ITaskDispatcher> dispatchers
    
    SimpleDispatcherAssignmentStrategy() {
        dispatchers = []
    }
    
    void setDispatchers(Collection<ITaskDispatcher> dispatchers) {
        this.dispatchers = dispatchers.asList()
    }
    
    boolean assign(LightestTestCase testcase) {
        assert dispatchers.size() > 0
        
        for (dispatcher in dispatchers) {
            if (dispatcher.getTestCase() == null) {
                dispatcher.setTestCase(testcase)
                testcase.setDispatcher(dispatcher)
                return true
            }
        }
        
        return false
    }
    
    void unassign(LightestTestCase testcase) {
        def dispatcher = testcase.getDispatcher()
        
        dispatcher.setTestCase(null)
        testcase.setDispatcher(null)
    }
}
